﻿using System;
using System.Globalization;
using System.Text;
using System.Windows.Data;

namespace QuailDashboard.View.Converters
{
    /// <summary>
    /// Converts the session's test name to a "pretty" name:
    /// removes the leading Test:
    /// inserts spaces before every capital letter
    /// </summary>
    public class TestNameConverter : IValueConverter
    {
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        /// <param name="value">The source data being passed to the target.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            var str = value.ToString();

            if (str.StartsWith("Test: "))
                str = str.Substring(6);

            bool islower = false;
            var result = new StringBuilder();

            foreach (char c in str)
            {
                if (char.IsUpper(c))
                {
                    if (!islower)
                        result.Append(c);
                    else
                    {
                        result.Append(' ');
                        result.Append(c);
                    }

                    islower = false;
                }
                else if (c == '_')
                {
                    islower = false;
                    result.Append(' ');
                }
                else
                {
                    if (!islower && result.Length > 1 && char.IsUpper(result[result.Length - 2]))
                        result.Insert(result.Length - 1, " ");

                    islower = true;
                    result.Append(c);
                }
            }

            return result.ToString();
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
        /// </summary>
        /// <returns>
        /// The value to be passed to the source object.
        /// </returns>
        /// <param name="value">The target data being passed to the source.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
